package mm.chenchen.modules.script.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.common.utils.FileUtils;
import mm.chenchen.common.utils.MinioClientUtil;
import mm.chenchen.common.utils.MinioUtil;
import mm.chenchen.constant.ResultMsg;
import mm.chenchen.modules.script.dao.ScriptMirroringDao;
import mm.chenchen.modules.script.dao.model.request.ScriptMirroringQuery;
import mm.chenchen.modules.script.entity.ScriptMirroring;
import mm.chenchen.modules.script.service.ScriptMirroringService;
import mm.chenchen.modules.sys.entity.SysUserEntity;
import mm.chenchen.utils.PageModel;
import mm.chenchen.utils.R;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service("scriptMirroringService")
////@DataSource("爬虫资源库")
public class ScriptMirroringServiceImpl extends ServiceImpl<ScriptMirroringDao, ScriptMirroring> implements ScriptMirroringService {

    @Autowired
    private MinioClientUtil minioClientUtil;


    public R findPage(Page<Map> page, ScriptMirroringQuery param) {
        IPage<ScriptMirroring> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            return R.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public R<List<ScriptMirroring>> dropDownList(ScriptMirroringQuery param) {
        QueryWrapper<ScriptMirroring> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ScriptMirroring::getName,param.getName());
        List<ScriptMirroring> list = list(queryWrapper);
        return R.ok().setData(list);
    }

    public R<ScriptMirroring> getById(Long id) {
        return R.ok().setData(super.getById(id));
    }

    ;

    public R saveInfo(ScriptMirroring data, MultipartFile file) throws Exception {

        String minioId = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String fileName=ObjectUtils.isEmpty("python") ? minioId : "python" + "/" + minioId;
        MinioUtil.upload(minioClientUtil.getMinioClient(), "scripts", file.getInputStream(), fileName);
        data.setType(FileUtils.getSuffix(file.getOriginalFilename()));
        data.setFileName(file.getOriginalFilename());
        data.setMinioId("python/"+minioId);
        data.setBucket("scripts");
        data.setCreateTime(new Date());
        data.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return R.ok().setData(super.save(data));
    }

    ;

    public R update(ScriptMirroring param) {
        ScriptMirroring data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
        return R.ok().setData(super.updateById(param));
    }

    ;

    public R deleteById(Long id) {
        ScriptMirroring data = this.getById(id).getData();
        if (ObjectUtils.isEmpty(data)) {
            return R.error("数据不存在!");
        }
        if (!ObjectUtils.isEmpty(data.getMinioId())) {
            MinioUtil.delete(minioClientUtil.getMinioClient(), data.getBucket(), data.getMinioId());
        }
        return R.ok().setData(super.removeById(id));
    }

    ;

}